﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMSSender.Entities.Models;
using SMSSender.Biz;
using SMSSender.DAL;
using System.Data.OleDb;
using SMSSender;
using SMSSender.Common;



namespace SMSSender
{
    public partial class SMSSender : Form
    {
        public int dem = 0;
        private int Flag = -1;
        protected string connectionStringName;
        public SMSSender()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;

            txtCustomer.Enabled = false;
            txtPhoneNumber.Enabled = false;
            txtDiachi.Enabled = false;

            cbTG.Enabled = false;
            cbVN.Enabled = false;
            cbSJC.Enabled = false;
            cbSBJ.Enabled = false;
            cbBTMC.Enabled = false;

            this.connectionStringName = null;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btGui_Click(object sender, EventArgs e)
        {            
            //khởi tạo giá trị ban đầu cho progress bar sử thuộc tính Value
            progressBar1.Value = progressBar1.Minimum;
            //khoảng tăng giữa các nấc trong ProgressBar
            progressBar1.Step = 1;
            //bắt đầu chạy
            dem = 0;
            if (txtNoidung.TextLength > 0)
            {
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    string v = null;
                    try { v = dataGridView1.Rows[j].Cells[1].Value.ToString(); }
                    catch { v = null; };
                    if (Convert.ToBoolean(dataGridView1.Rows[j].Cells[0].Value)== true && !string.IsNullOrEmpty(v))
                    {
                        for (int i = 0; i <= 160; i++)
                        {
                            //thi hành tăng ProgressBar     
                            progressBar1.PerformStep();
                        }
                            //MessageBox.Show(dataGridView1.Rows[j].Cells[1].Value.ToString());
                            progressBar1.PerformStep();

                            MessageHistoryInfo messageHistoryInfo = new MessageHistoryInfo();
                            messageHistoryInfo.IdMessage = Guid.NewGuid().ToString();
                            messageHistoryInfo.PostDate = DateTime.Now;
                            messageHistoryInfo.Sender = "01649782489";
                            messageHistoryInfo.Reciver = dataGridView1.Rows[j].Cells[1].Value.ToString();
                            messageHistoryInfo.Conten = txtNoidung.Text;
                            messageHistoryInfo.Status = true;
                            messageHistoryInfo.Flag = true;
                            new MessageHistoryService().Insert(messageHistoryInfo);
                            dem++;
                            
                        }
                }
                if (Convert.ToBoolean(dataGridView1.CurrentRow.Cells[0].Value) == true)
                    {
                        MessageBox.Show("Completed!","Thông báo");                
                    }
            }
            else
                
                MessageBox.Show("Chưa nhập nội dung tin nhắn. Mời nhập!","Chú ý");
            if (Convert.ToBoolean(dataGridView1.CurrentRow.Cells[0].Value) == false)
            {
                MessageBox.Show("Chưa chọn khách hàng!","Chú ý"); 
            }
            this.BinData2();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Flag = 3;
            mo.Filter = "(*.xls)|*.xls";
            System.Data.DataTable bang = new DataTable();
            if (mo.ShowDialog() == DialogResult.OK)
            {
                if (mo.FileName != "")
                {
                    mo_excel excel = new SMSSender.mo_excel(mo.FileName);
                    bang = excel.get_data_from_excell();
                    dataGridView1.DataSource = bang;
                    btLuu.Enabled = true;
                    tool.style_dgview(dataGridView1);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtNoidung.Focus();
            lbSokytu.Text = "0";
            //bắt buộc nấc trong progress bar xuất phát từ số 1 sử dụng thuộc tính Minimum
            progressBar1.Minimum = 1;
            //và chỉ cho phép nấc này chạy đến giá trị tối đa là 160 sử dụng thuộc tính Maximum
            progressBar1.Maximum = 160;
            
            this.BinData();
            this.BinData2();
        }

        private void txtNoidung_TextChanged(object sender, EventArgs e)
        {
            if (txtNoidung.TextLength > 160)
            { 
                MessageBox.Show("Bạn đã nhập quá ký tự cho phép (160)!","Cảnh báo");
                txtNoidung.Text = txtNoidung.Text.Remove(txtNoidung.TextLength - 1);
                return;
            }
            lbSokytu.Text = txtNoidung.TextLength.ToString();
            
        }

        private void cbChon_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (cbChon.Checked ==true)
                {
                    dataGridView1.Rows[i].Cells[0].Value = true;                    
                }
                else
                {
                    dataGridView1.Rows[i].Cells[0].Value = false;
                }                
            }
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            CustomerInfo customerInfo = new CustomerInfo();
            CustomerService customerService = new CustomerService();
            
            CustomerRoleInfo customerRoleInfo = new CustomerRoleInfo();
            CustomerRoleService customerRoleService = new CustomerRoleService();

            CustomerRoleHistoryInfo customerRoleHistoryInfo = new CustomerRoleHistoryInfo();
            CustomerRoleHistoryService customerRoleHistoryService = new CustomerRoleHistoryService();
            switch (Flag)
            {
                case 1:
                    //Thêm
                    
                    try
                    {
                        customerInfo.NameCustomer = txtCustomer.Text;
                        customerInfo.PhoneNumber = txtPhoneNumber.Text;
                        customerInfo.Address = txtDiachi.Text;
                        customerInfo.IdPhone = customerInfo.PhoneNumber;
                        if (txtPhoneNumber.TextLength > 0 && customerService.Select(customerInfo.IdPhone).Count == 0)
                        {
                            customerService.Insert(customerInfo);
                            MessageBox.Show("Succeeded!","Thông báo");
                            if (cbSBJ.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "SBJ";
                                customerRoleInfo.RoleName = "Giá vàng SBJ";                                
                                customerRoleInfo.IdPhone = customerInfo.PhoneNumber;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                customerRoleService.Insert(customerRoleInfo);
                            }
                            if (cbBTMC.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "BTMC";
                                customerRoleInfo.RoleName = "Giá vàng BTMC";                                
                                customerRoleInfo.IdPhone = customerInfo.PhoneNumber;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                customerRoleService.Insert(customerRoleInfo);
                            }
                            if (cbSJC.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "SJC";
                                customerRoleInfo.RoleName = "Giá vàng SJC";                                
                                customerRoleInfo.IdPhone = customerInfo.PhoneNumber;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                customerRoleService.Insert(customerRoleInfo);
                            }
                            if (cbTG.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "TG";
                                customerRoleInfo.RoleName = "Giá vàng Thế giới";                                
                                customerRoleInfo.IdPhone = customerInfo.PhoneNumber;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                customerRoleService.Insert(customerRoleInfo);
                            }
                            if (cbVN.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "VN";
                                customerRoleInfo.RoleName = "Giá vàng Việt Nam";                                
                                customerRoleInfo.IdPhone = customerInfo.PhoneNumber;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                customerRoleService.Insert(customerRoleInfo);
                            }
                        }
                        if (txtPhoneNumber.TextLength == 0) MessageBox.Show("Chưa thêm khách hàng!","Chú ý");
                    }
                    catch { MessageBox.Show("Identical key!","Thông báo"); };
                    Flag = -1;
                    break;
                case 2:
                    //Sửa
                    try
                    {
                            if (cbSBJ.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "SBJ";
                                customerRoleInfo.RoleName = "Giá vàng SBJ";
                                customerRoleInfo.IdPhone = txtPhoneNumber.Text;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                if (customerRoleService.Select(customerRoleInfo.IdRole).Count == 0)
                                {
                                    customerRoleService.Insert(customerRoleInfo);
                                }
                            }
                            if (cbBTMC.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "BTMC";
                                customerRoleInfo.RoleName = "Giá vàng BTMC";
                                customerRoleInfo.IdPhone = txtPhoneNumber.Text;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                if (customerRoleService.Select(customerRoleInfo.IdRole).Count == 0)
                                {
                                    customerRoleService.Insert(customerRoleInfo);                                    
                                }
                                
                            }
                            if (cbSJC.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "SJC";
                                customerRoleInfo.RoleName = "Giá vàng SJC";
                                customerRoleInfo.IdPhone = txtPhoneNumber.Text;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                if (customerRoleService.Select(customerRoleInfo.IdRole).Count == 0)
                                {
                                    customerRoleService.Insert(customerRoleInfo);
                                }
                               
                            }
                            if (cbTG.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "TG";
                                customerRoleInfo.RoleName = "Giá vàng Thế giới";
                                customerRoleInfo.IdPhone = txtPhoneNumber.Text;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                if (customerRoleService.Select(customerRoleInfo.IdRole).Count == 0)
                                {
                                    customerRoleService.Insert(customerRoleInfo);
                                }
                                
                            }
                            if (cbVN.Checked == true)
                            {
                                customerRoleInfo.RoleKey = "VN";
                                customerRoleInfo.RoleName = "Giá vàng Việt Nam";
                                customerRoleInfo.IdPhone = txtPhoneNumber.Text;
                                customerRoleInfo.Role = true;
                                customerRoleInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                if (customerRoleService.Select(customerRoleInfo.IdRole).Count == 0)
                                {
                                    customerRoleService.Insert(customerRoleInfo);
                                }
                                
                            }
                        //}
                    }
                    catch { };
                    List<CustomerRoleInfo> CustomerRoleInfoLst = null;
                    try
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            if (cbTG.Checked == false && (dataGridView1.Rows[i].Cells[0].Selected || dataGridView1.Rows[i].Cells[1].Selected || dataGridView1.Rows[i].Cells[2].Selected || dataGridView1.Rows[i].Cells[3].Selected || dataGridView1.Rows[i].Cells[4].Selected))
                            {
                                customerRoleInfo.RoleKey = "TG";
                                customerRoleInfo.IdPhone = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                CustomerRoleInfoLst = customerRoleService.SelectWithIdPhoneAndNotRolKey(dataGridView1.Rows[i].Cells[1].Value.ToString(), "TG");
                                if (CustomerRoleInfoLst.Count > 0)
                                {
                                    customerRoleHistoryInfo.IdAo = Guid.NewGuid();
                                    customerRoleHistoryInfo.Action = "Xóa";
                                    customerRoleHistoryInfo.Postdate = DateTime.Now;
                                    customerRoleHistoryInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.IdPhone = customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.RoleName = "Giá vàng thế giới";
                                    customerRoleHistoryInfo.Role = "TG";
                                    customerRoleHistoryService.Insert(customerRoleHistoryInfo);

                                }
                                customerRoleService.DeleteIdPhone(customerRoleInfo.IdPhone, customerRoleInfo.RoleKey);
                            }
                            if (cbVN.Checked == false && (dataGridView1.Rows[i].Cells[0].Selected || dataGridView1.Rows[i].Cells[1].Selected || dataGridView1.Rows[i].Cells[2].Selected || dataGridView1.Rows[i].Cells[3].Selected || dataGridView1.Rows[i].Cells[4].Selected))
                            {
                                customerRoleInfo.RoleKey = "VN";
                                customerRoleInfo.IdPhone = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                CustomerRoleInfoLst = customerRoleService.SelectWithIdPhoneAndNotRolKey(dataGridView1.Rows[i].Cells[1].Value.ToString(), "VN");
                                if (CustomerRoleInfoLst.Count > 0)
                                {
                                    customerRoleHistoryInfo.IdAo = Guid.NewGuid();
                                    customerRoleHistoryInfo.Action = "Xóa";
                                    customerRoleHistoryInfo.Postdate = DateTime.Now;
                                    customerRoleHistoryInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.IdPhone = customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.RoleName = "Giá vàng Việt Nam";
                                    customerRoleHistoryInfo.Role = "VN";
                                    customerRoleHistoryService.Insert(customerRoleHistoryInfo);

                                }
                                customerRoleService.DeleteIdPhone(customerRoleInfo.IdPhone, customerRoleInfo.RoleKey);
                            }
                            if (cbSJC.Checked == false && (dataGridView1.Rows[i].Cells[0].Selected || dataGridView1.Rows[i].Cells[1].Selected || dataGridView1.Rows[i].Cells[2].Selected || dataGridView1.Rows[i].Cells[3].Selected || dataGridView1.Rows[i].Cells[4].Selected))
                            {
                                customerRoleInfo.RoleKey = "SJC";
                                customerRoleInfo.IdPhone = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                CustomerRoleInfoLst = customerRoleService.SelectWithIdPhoneAndNotRolKey(dataGridView1.CurrentRow.Cells[1].Value.ToString(), "SJC");
                                if (CustomerRoleInfoLst.Count > 0)
                                {
                                    customerRoleHistoryInfo.IdAo = Guid.NewGuid();
                                    customerRoleHistoryInfo.Action = "Xóa";
                                    customerRoleHistoryInfo.Postdate = DateTime.Now;
                                    customerRoleHistoryInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.IdPhone = customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.RoleName = "Giá vàng SJC";
                                    customerRoleHistoryInfo.Role = "SJC";
                                    customerRoleHistoryService.Insert(customerRoleHistoryInfo);
                                }
                                customerRoleService.DeleteIdPhone(customerRoleInfo.IdPhone, customerRoleInfo.RoleKey);
                            }
                            if (cbSBJ.Checked == false && (dataGridView1.Rows[i].Cells[0].Selected || dataGridView1.Rows[i].Cells[1].Selected || dataGridView1.Rows[i].Cells[2].Selected || dataGridView1.Rows[i].Cells[3].Selected || dataGridView1.Rows[i].Cells[4].Selected))
                            {
                                customerRoleInfo.RoleKey = "SBJ";
                                customerRoleInfo.IdPhone = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                CustomerRoleInfoLst = customerRoleService.SelectWithIdPhoneAndNotRolKey(dataGridView1.Rows[i].Cells[1].Value.ToString(), "SBJ");
                                if (CustomerRoleInfoLst.Count > 0)
                                {
                                    customerRoleHistoryInfo.IdAo = Guid.NewGuid();
                                    customerRoleHistoryInfo.Action = "Xóa";
                                    customerRoleHistoryInfo.Postdate = DateTime.Now;
                                    customerRoleHistoryInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.IdPhone = customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.RoleName = "Giá vàng SBJ";
                                    customerRoleHistoryInfo.Role = "SBJ";
                                    customerRoleHistoryService.Insert(customerRoleHistoryInfo);

                                }
                                customerRoleService.DeleteIdPhone(customerRoleInfo.IdPhone, customerRoleInfo.RoleKey);
                            }
                            if (cbBTMC.Checked == false && (dataGridView1.Rows[i].Cells[0].Selected || dataGridView1.Rows[i].Cells[1].Selected || dataGridView1.Rows[i].Cells[2].Selected || dataGridView1.Rows[i].Cells[3].Selected || dataGridView1.Rows[i].Cells[4].Selected))
                            {
                                customerRoleInfo.RoleKey = "BTMC";
                                customerRoleInfo.IdPhone = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                CustomerRoleInfoLst = customerRoleService.SelectWithIdPhoneAndNotRolKey(dataGridView1.Rows[i].Cells[1].Value.ToString(), "BTMC");
                                if (CustomerRoleInfoLst.Count > 0)
                                {
                                    customerRoleHistoryInfo.IdAo = Guid.NewGuid();
                                    customerRoleHistoryInfo.Action = "Xóa";
                                    customerRoleHistoryInfo.Postdate = DateTime.Now;
                                    customerRoleHistoryInfo.IdRole = customerRoleInfo.RoleKey + customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.IdPhone = customerRoleInfo.IdPhone;
                                    customerRoleHistoryInfo.RoleName = "Giá vàng BTMC";
                                    customerRoleHistoryInfo.Role = "BTMC";
                                    customerRoleHistoryService.Insert(customerRoleHistoryInfo);

                                }
                                customerRoleService.DeleteIdPhone(customerRoleInfo.IdPhone, customerRoleInfo.RoleKey);
                            }
                        }
                    }
                    catch { };

                    customerInfo.NameCustomer = txtCustomer.Text;
                    customerInfo.PhoneNumber = txtPhoneNumber.Text;
                    customerInfo.Address = txtDiachi.Text;
                    customerInfo.IdPhone = customerInfo.PhoneNumber;
                    customerService.Update(customerInfo);
                    Flag = -1;
                    MessageBox.Show("Succeeded!", "Thông báo");
                    break;
                case 3:
                    //Import data
                    bool label = false;
                    dem = 0;
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            string v = null; 
                            try
                            {
                                v = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                customerInfo.PhoneNumber = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                customerInfo.NameCustomer = dataGridView1.Rows[i].Cells[2].Value.ToString();
                                customerInfo.Address = dataGridView1.Rows[i].Cells[3].Value.ToString();
                                customerInfo.IdPhone = customerInfo.PhoneNumber;
                                if (customerService.Select(customerInfo.IdPhone).Count == 0&&v.Length > 9 && Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
                                {
                                    customerService.Insert(customerInfo);
                                    dem++;
                                    label = true;
                                }
                                else
                                    if (customerService.Select(customerInfo.IdPhone).Count != 0 && v.Length > 9 && Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true) 
                                    {
                                        MessageBox.Show("Số điện thoại " + dataGridView1.Rows[i].Cells[1].Value.ToString()+" đã tồn tại!");
                                        label = false;
                                    }
                            }
                            catch
                            {
                                v = null;
                            };
                        }
                        if (label == true)
                        {
                            MessageBox.Show("Succeeded "+dem+" !","Thông báo");
                        }
                        else { }
                        
                    Flag = -1;
                    break;
            }
            cbBTMC.Enabled = false;
            cbSBJ.Enabled = false;
            cbSJC.Enabled = false;
            cbTG.Enabled = false;
            cbVN.Enabled = false;
            this.BinData();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            Flag = 1;//Thêm
            txtCustomer.Clear();            
            txtDiachi.Clear();
            txtPhoneNumber.Clear();
            txtCustomer.Focus();

            txtCustomer.Enabled = true;
            txtPhoneNumber.Enabled = true;
            txtDiachi.Enabled = true;

            cbBTMC.Enabled = true;
            cbSBJ.Enabled = true;
            cbSJC.Enabled = true;
            cbTG.Enabled = true;
            cbVN.Enabled = true;

            cbBTMC.Checked = false;
            cbSBJ.Checked = false;
            cbSJC.Checked = false;
            cbTG.Checked = false;
            cbVN.Checked = false;
        }
        protected void BinData()
        {
            CustomerInfo customerInfo = new CustomerInfo();
            CustomerService customerService = new CustomerService();
            dataGridView1.DataSource = customerService.SelectAll();
            
        }
        protected void BinData2()
        {
            dataGridView2.DataSource = new MessageHistoryService().SelectAll();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            Flag = 2;//Sửa
            txtCustomer.Enabled = true;
            txtPhoneNumber.Enabled = true;
            txtDiachi.Enabled = true;

            cbBTMC.Enabled = true;
            cbSBJ.Enabled = true;
            cbSJC.Enabled = true;
            cbTG.Enabled = true;
            cbVN.Enabled = true;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            CustomerInfo customerInfo = new CustomerInfo();
            CustomerService customerService = new CustomerService();

            CustomerRoleInfo customerRoleInfo = new CustomerRoleInfo();
            CustomerRoleHistoryInfo customerRoleHistoryInfo = new CustomerRoleHistoryInfo();
            CustomerRoleHistoryService customerRoleHistoryService = new CustomerRoleHistoryService();

            CustomerHistoryInfo customerHistoryInfo = new CustomerHistoryInfo();
            CustomerHistoryService customerHistoryService = new CustomerHistoryService();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            { 
                if (dataGridView1.Rows[i].Cells[4].Selected)
                {
                    DialogResult Dr = MessageBox.Show("Bạn chắc chắn muốn xóa bản ghi này ?", "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (Dr == DialogResult.Yes)
                    {
                        customerRoleInfo.IdPhone = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        new CustomerRoleService().Delete(customerRoleInfo.IdPhone);
                        customerInfo.IdPhone = dataGridView1.Rows[i].Cells[5].Value.ToString();
                        customerService.Delete(customerInfo.IdPhone);
                    }
                    customerHistoryInfo.IdAo = Guid.NewGuid();
                    customerHistoryInfo.Action = "Xóa";
                    customerHistoryInfo.Postdate = DateTime.Now;
                    customerHistoryInfo.IdPhone = customerInfo.IdPhone;
                    customerHistoryInfo.PhoneNumber = customerRoleInfo.IdPhone;
                    customerHistoryInfo.NameCustomer = txtCustomer.Text;
                    customerHistoryInfo.Address = txtDiachi.Text;
                    customerHistoryService.Insert(customerHistoryInfo);

                    txtCustomer.Clear();
                    txtPhoneNumber.Clear();
                    txtDiachi.Clear();
                    this.BinData();
                }
            }

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            txtCustomer.Text = dataGridView1[2, dataGridView1.CurrentRow.Index].Value.ToString();
            txtPhoneNumber.Text = dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString();
            txtDiachi.Text = dataGridView1[3, dataGridView1.CurrentRow.Index].Value.ToString();

            cbTG.Checked = false;
            cbVN.Checked = false;
            cbSJC.Checked = false;
            cbSBJ.Checked = false;
            cbBTMC.Checked = false;
            List<V_ListPhoneSendSMSInfo> vListPhoneSendSMSInfo = new V_ListPhoneSendSMSService().SelectListPhoneSendSMSWithPhoneNumber(dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString());
            if (vListPhoneSendSMSInfo.Count > 0)
            {
                for (int i = 0; i < vListPhoneSendSMSInfo.Count; i++)
                {
                    switch (vListPhoneSendSMSInfo[i].RoleKey)
                    {
                        case "TG":
                            cbTG.Checked = true;
                            break;
                        case "VN":
                            cbVN.Checked = true;
                            break;
                        case "SJC":
                            cbSJC.Checked = true;
                            break;
                        case "SBJ":
                            cbSBJ.Checked = true;
                            break;
                        case "BTMC":
                            cbBTMC.Checked = true;
                            break;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.BinData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.BinData2();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageHistoryInfo messageHistoryInfo = new MessageHistoryInfo();
            MessageHistoryService messageHistoryService = new MessageHistoryService();

            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                if (dataGridView2.Rows[i].Cells[5].Selected)
                {
                    messageHistoryInfo.IdMessage =dataGridView2.Rows[i].Cells[6].Value.ToString();
                    DialogResult Dr = MessageBox.Show("Bạn chắc chắn muốn xóa bản ghi này ?", "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (Dr == DialogResult.Yes)
                    {
                        messageHistoryService.Delete(messageHistoryInfo.IdMessage);
                    }
                    this.BinData2();
                }
            }

        }

        //AccessData accessData = new AccessData();
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                this.BinData();
            }
            else
            {
                if (rbCustomer.Checked)
                {
                    dataGridView1.DataSource = accessData.GetTable("Select * from Customer where NameCustomer like N'%" + textBox1.Text + "%'");
                }
                else
                    if (rbPhoneNumber.Checked)
                    {
                        dataGridView1.DataSource = accessData.GetTable("Select * from Customer where PhoneNumber like N'%" + textBox1.Text + "%'");
                    }
            }
        }

        
    }
}
